What is eslint-plugin-vue?
The eslint-plugin-vue npm package is a plugin for ESLint specifically designed for Vue.js applications. It provides linting rules that are tailored to the Vue.js framework, helping developers to write consistent and error-free code by following best practices and avoiding common mistakes.
What are eslint-plugin-vue's main functionalities?
Style Guide Enforcement
Enforces a style guide by limiting the number of attributes per line in Vue templates. This helps in maintaining readability and consistency in the codebase.
"vue/max-attributes-per-line": ["error", { "singleline": 3, "multiline": { "max": 1, "allowFirstLine": false } }]
Component Naming
Ensures that component names in templates are written in a specific case, such as PascalCase, which is a common convention in Vue.js projects.
"vue/component-name-in-template-casing": ["error", "PascalCase"]
Avoiding Deprecated Features
Prevents the use of deprecated features like the 'slot' attribute, encouraging developers to use the updated syntax and features of Vue.js.
"vue/no-deprecated-slot-attribute": "error"
Prop Validation
Ensures that every prop has a default value, which can prevent bugs and make the components more predictable.
"vue/require-default-prop": "error"
Script Indentation
Enforces consistent indentation in the script section of Vue single-file components, which improves code readability and maintainability.
"vue/script-indent": ["error", 2, { "baseIndent": 1, "switchCase": 1, "ignores": [] }]
Other packages similar to eslint-plugin-vue
eslint-plugin-react
Similar to eslint-plugin-vue, eslint-plugin-react provides linting rules specific to React applications. It helps enforce best practices and conventions for React components, JSX syntax, and more.
eslint-plugin-angular
This plugin is designed for Angular applications and offers linting rules that cater to the Angular framework's structure and coding styles, similar to how eslint-plugin-vue serves Vue.js developers.
eslint-plugin-svelte3
For developers using Svelte, eslint-plugin-svelte3 provides linting rules tailored to the Svelte framework. It helps maintain code quality and consistency in a way that's analogous to eslint-plugin-vue's role for Vue.js.
eslint-plugin-vue
Official ESLint plugin for Vue.js
:book: Documentation
See the official website.
:anchor: Versioning Policy
This plugin is following Semantic Versioning and ESLint's Semantic Versioning Policy.
:newspaper: Changelog
This project uses GitHub Releases.
:beers: Contribution Guide
Contribution is welcome!
See The ESLint Vue Plugin Developer Guide.
Working with Rules
Before you start writing a new rule, please read the official ESLint guide.
Next, in order to get an idea how does the AST of the code that you want to check looks like, use one of the following applications:
- astexplorer.net - the best tool to inspect ASTs, but it doesn't support Vue template yet
- ast.js.org - not fully featured, but supports Vue template syntax
Since single file components in Vue are not plain JavaScript, the default parser couldn't be used, so a new one was introduced. vue-eslint-parser
generates enhanced AST with nodes that represent specific parts of the template syntax, as well as what's inside the <script>
tag.
To know more about certain nodes in produced ASTs, go here:
The vue-eslint-parser
provides a few useful parser services that help traverse the produced AST and access tokens of the template:
context.parserServices.defineTemplateBodyVisitor(visitor, scriptVisitor)
context.parserServices.getTemplateBodyTokenStore()
Check out an example rule to get a better understanding of how these work.
Please be aware that regarding what kind of code examples you'll write in tests, you'll have to accordingly set up the parser in RuleTester
(you can do it on a per test case basis). See an example here.
If you'll stuck, remember there are plenty of rules you can learn from already. If you can't find the right solution, don't hesitate to reach out in issues – we're happy to help!
:lock: License
See the LICENSE file for license rights and limitations (MIT).